package security;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

import common.impl.SpringContextUtil;

public class MySecurityContextHandle {

	/**
	 * 将微信用户openId存入session中
	 */
	public static void setIsAuth(boolean isAuth) {
		HttpSession session = ServletActionContext.getRequest().getSession();
		if (null != session)
			if (isAuth)
				session.setAttribute("isAuth", "1");
			else
				session.setAttribute("isAuth", "0");
	}

	public static String getIsAuth() {
		HttpSession session = ServletActionContext.getRequest().getSession();
		if (null != session) {
			Object o = session.getAttribute("isAuth");
			if (null != o)
				return (String) o;
		}
		return null;
	}

	/**
	 * 获取游戏开始时间
	 * 
	 * @return
	 */
	public static String getStartTime() {
		HttpSession session = ServletActionContext.getRequest().getSession();
		if (null != session) {
			Object o = session.getAttribute("startTime");
			if (null != o)
				return (String) o;
		}
		return null;
	}

	/**
	 * 从session中获取微信用户openId
	 */
	public static String getOpenId() {
		// return "orkGVw5abqgC2mq-rRCkZCq2ZPMQ";
		HttpSession session = ServletActionContext.getRequest().getSession();
		if (null != session) {
			Object o = session.getAttribute("openId");
			if (null != o)
				return (String) o;
		}
		return null;
	}

	/**
	 * 验证是否是post请求
	 */
	public static String getMethod() {
		/*
		 * HttpServletRequest request = ServletActionContext.getRequest();
		 * String method = request.getMethod(); String referer =
		 * request.getHeader("Referer"); String agent =
		 * request.getHeader("User-Agent").toLowerCase(); if
		 * (method.equals("get") || method.equals("GET") ||
		 * !agent.contains("micromessenger") || StringUtils.isEmpty(referer))
		 * throw new ServiceException("ERR-0003");
		 */
		return null;
	}

	/**
	 * 将微信用户openId存入session中
	 */
	public static void setOpenId(String openId) {
		HttpSession session = ServletActionContext.getRequest().getSession();
		if (null != session)
			session.setAttribute("openId", openId);
	}

	/**
	 * 获取用户Id
	 * 
	 * @return
	 */
	public static Long getUserId() {
		Object principal = SecurityContextHolder.getContext()
				.getAuthentication().getPrincipal();
		if (principal instanceof MyUserDetails) {
			return ((MyUserDetails) principal).getUserId();
		} else {
			return null;
		}
	}

	public static void setUserId(Long userid) {
		Object principal = SecurityContextHolder.getContext()
				.getAuthentication().getPrincipal();

		if (principal instanceof UserDetails)
			((MyUserDetails) principal).setUserId(userid);
	}

	/**
	 * 从session中获取值
	 */
	public static String getItem(String key) {
		if (StringUtils.isEmpty(key))
			return null;
		HttpSession session = ServletActionContext.getRequest().getSession();
		if (null != session) {
			Object o = session.getAttribute(key);
			if (null != o)
				return (String) o;
		}
		return null;
	}

	/**
	 * 将键值对存入session中
	 */
	public static void setItem(String key, String value) {
		if (StringUtils.isEmpty(key))
			return;
		if (StringUtils.isEmpty(value))
			return;
		HttpSession session = ServletActionContext.getRequest().getSession();
		if (null != session)
			session.setAttribute(key, value);
	}

	/**
	 * 从session中删除值
	 */
	public static String removeItem(String key) {
		if (StringUtils.isEmpty(key))
			return null;
		HttpSession session = ServletActionContext.getRequest().getSession();
		if (null != session) {
			Object o = session.getAttribute(key);
			if (null != o) {
				session.removeAttribute(key);
				return (String) o;
			}
		}
		return null;
	}

	/**
	 * 添加authentication
	 * 
	 * @param userName
	 *            用户名
	 * @param password
	 *            密码
	 * @return
	 */
	public static Boolean addAuthentication(String userName, String password) {
		UserPwdCodeAuthenticationFilter af = (UserPwdCodeAuthenticationFilter) SpringContextUtil
				.getBean("loginFilter");
		Authentication auth = af.myAuthMethod(
				ServletActionContext.getRequest(), ServletActionContext
						.getResponse(), userName, password);
		if (null != auth) {
			SecurityContextHolder.getContext().setAuthentication(auth);
			return true;
		}
		return false;
	}
}
